var Customer = function (customer) {
  // 继承BaseModel
  BaseModel.call(this);
  var self = this;
  var copyFields = ['id', 'customerName', 'customerCode', 'status', 'creater',
    'creationTime', 'lastModifier', 'lastModifyTime', 'company',
    'contact', 'phone', 'fax', 'address', 'paymentWay', 'type', 'password', 'isTax',
    'preight'
  ];
  var fn = {
    // 初始化属性
    initData: function (customer) {
      if (customer === undefined) {
        customer = {
          paymentWay: '1',
          status: '使用',
          isTax: 0,
          preight: 1
        };
      }
      self.setDataFromObj(customer, copyFields, '');

      var email = customer.email;
      if (email) {
        email = email.join('\n');
      } else {
        email = '';
      }
      self.email = email;
    },

    getErrorMsg: function (code) {
      var errorObj = {
        customerCode: "请输入客户代码",
        customerName: "请输入客户名称",
        type: "请选择客户类型",
        contact: "请输入联系人",
        paymentWay: "请选择付款结算方式",
        status: "请选择状态"
      };
      return errorObj[code];
    }
  };

  this.checkValid = function () {
    var requiredFields = ['customerCode', 'customerName', 'type', 'contact', 'paymentWay', 'status'];
    if (!CheckVaildUtil.checkNullValue(requiredFields, this, fn.getErrorMsg)) {
      return false;
    }
    return true;
  };

  this.checkEmailValid = function (email, emailReg) {
    if (email === '') {
      return true;
    }

    var emails = email.split(';');
    for (var i = 0; i < emails.length; i++) {
      var data = emails[i];
      if (!emailReg.test(data)) {
        return false;
      }
    }
    return true;
  };

  this.getObject = function () {
    var postData = {};
    if (this.id !== '') {
      postData.id = this.id;
    }
    this.setPostData(postData, copyFields, true, true);

    var email = self.email.trim();
    if (email !== '') {
      email = email.split(/[(\r\n)\r\n;]+/g);
      email = _.uniq(email);
      var newEmail = [];
      for (var i = 0; i < email.length; i++) {
        var value = email[i].trim();
        if (value !== '') {
          newEmail.push(value);
        }
      }
      email = newEmail.join(';');
    }
    postData.email = email;

    return postData;
  };

  fn.initData(customer);
};
